<template>
	<view class="my">
		<!-- 顶部结构 -->
		<view class="nav" @click="isShowAddition = true">
			<view class="img-box">
				<image :src="bgImg" mode="" class="img"></image>
			</view>
		</view>
		<!-- 顶部结构 -->
		<!-- 遮罩层 -->
		<view class="popup-box">
			<view class="popup-title" @click="isShowAddition = true">
				<view class="left">
					<view class="img-box">
						<image :src="userImg" mode="widthFix" class="img"></image>
					</view>
				</view>
				<view class="right">
					<view class="nickName">
						<text>{{userName}}</text>
					</view>
					<view class="text">
						<text>{{userBrief}}</text>
					</view>
				</view>
			</view>
			<view class="popup-pages-box">
				<view class="popup-pages" v-for="(item,index) in list" :key="item.id" @click="goTo(item.url)">
					<view class="popup-pages-left">
						{{item.title}}
					</view>
					<view class="popup-pages-right">
						&gt;
					</view>
				</view>
			</view>

		</view>
		<!-- 遮罩层 -->

		<u-popup v-model="isShowAddition" mode="bottom">
			<u-button @click="updateUserBg">更换背景</u-button>
			<u-button @click="useChangnAvatar">更换头像</u-button>
			<u-button @click="goTo('/pages/my/proile/safety/safety')">安全中心</u-button>
		</u-popup>
	</view>
</template>

<script>
	import {
		pathToBase64,
		base64ToPath
	} from 'image-tools'
	export default {
		data() {
			return {
				isShowAddition: false,
				userName: '',
				userBrief: '',
				userImg: '../../static/images/my.png',
				bgImg:'../../static/images/bg.png',
				list: [{
						id: 1,
						title: "个人资料",
						url: "./proile/data/data"
					},
					{
						id: 2,
						title: "我的订单",
						url: "./proile/order/order"
					},
					{
						id: 3,
						title: "我的收藏",
						url: "./proile/collection/collection"
					},
					{
						id: 4,
						title: "地址管理",
						url: "./proile/address/address"
					},
				]
			}
		},
		onReady() {
			this.getRegister()
		},
		methods: {
			// 上传背景图
			updateUserBg(){
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						pathToBase64(tempFilePaths[0])
						
							.then(base64 => {
								let serverBase64Img = base64.replace(/^data:image\/[A-Za-z]+;base64,/, '')
								this.$request({
									url:'/updateUserBg',
									method:'POST',
									data:{
										imgType:'jpg',
										serverBase64Img
									},
									success:res => {
										this.isShowAddition = false
										this.getRegister()
										if(res.data.code == 'H001'){
											uni.userImg = res.data.userImg
											uni.showToast({
											    title: '成功',
											    duration: 2000
											})
										}
									}
								})
							})
							.catch(error => {
								// console.error(error)
							})
					}
				})
			},
			// 更换用户头像
			useChangnAvatar() {
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						pathToBase64(tempFilePaths[0])
						
							.then(base64 => {
								let serverBase64Img = base64.replace(/^data:image\/[A-Za-z]+;base64,/, '')
								this.$request({
									url:'/updateAvatar',
									method:'POST',
									data:{
										imgType:'jpg',
										serverBase64Img
									},
									success:res => {
										this.isShowAddition = false
										this.getRegister()
										if(res.data.code == 'H001'){
											uni.userImg = res.data.userImg
											uni.showToast({
											    title: '成功',
											    duration: 2000
											})
										}
									}
								})
							})
							.catch(error => {
								// console.error(error)
							})
					}
				})
			},
			// 获取用户信息
			getRegister() {
				uni.showLoading({
					title: "正在加载...",
					icon: "none",
					mask: true
				})
				this.$request({
					url: "/findMy",
					success: (res) => {
						// console.log(res)
						if (res.statusCode === 200) {
							uni.hideLoading()
							if (res.data.code === "A001") {
								this.userBrief = res.data.result[0].desc || '这家伙很懒，什么都没留下！'
								this.userName = res.data.result[0].nickName || '小瑞幸'
								this.userImg = res.data.result[0].userImg
								this.bgImg = res.data.result[0].userBg
							}
							if (res.data.code === 700) {
								uni.showToast({
									title: res.data.msg,
									icon: "none",
									mask: true,
									duration: 1000
								})
								setTimeout(() => {
									uni.navigateTo({
										url: "../login/login"
									})
								}, 1000)

							}

						}
					}
				})
			},
			goTo(url) {
				uni.navigateTo({
					url: url
				})
			}
		},
	}
</script>
<style lang="less" scoped>
	.my {
		background-color: #f7f7f7;

		.nav {
			.img-box {
				width: 100%;
				height: 400rpx;

				.img {
					width: 100%;
					height: 100%;
				}
			}
		}

		.popup-box {
			position: relative;
			top: -100rpx;
			border-radius: 30rpx 30rpx 0 0;
			padding: 30rpx;
			margin: 0 30rpx;
			background-image: linear-gradient(rgba(255, 255, 255, .6), #fff, #fff);

			.popup-title {
				display: flex;

				.left {
					.img-box {
						width: 150rpx;
						height: 150rpx;
						border-radius: 50%;
						border: 1rpx solid gray;
						overflow: hidden;

						.img {
							width: 100%;
							height: 100%;
						}
					}
				}

				.right {
					margin-left: 30rpx;

					.nickName {
						color: #0c34ba;
						font-size: 36rpx;
						font-weight: bold;
					}

					.text {
						color: #999;
						font-size: 32rpx;
						margin: 30rpx 0;
					}
				}

			}

			.popup-pages-box {
				padding-bottom: 30rpx;
				margin-top: 150rpx;

				.popup-pages {
					color: #999;
					border-bottom: 2rpx solid #999;
					padding: 30rpx 0;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.popup-pages-left {
						font-size: 34rpx;
					}

					.popup-pages-right {}
				}
			}
		}

	}
</style>
